import LogFactory from "../log/LogFactory.js";

const log = LogFactory.getLogger("ScoreForm");
/**
 * 计分板表单
 */
export default class ScoreForm {
    changSelect = null;
    junSelect = null;
    maSelect = null;
    formSpinner = null;
    formButton = null;
    formCatd = null;
    formErrorMsg = null;
    formErrorAlert = null;
    isError = false;

    init() {
        // log.debug("初始化计分板表单");
        this.bindElement();
    }

    /**
     * 绑定元素
     */
    bindElement() {
        this.changSelect = $("#inputGroupSelectChang");
        this.maSelect = $("#inputGroupSelectMa");
        this.junSelect = $("#inputGroupSelectJun");
        this.formSpinner = $("#scoreCommitButtonSpinner");
        this.formButton = $("#scoreCommitButton");
        this.formCatd = $("#formCard");
        this.formErrorAlert = $("#formErrorAlert");
        this.formErrorMsg = $("#formErrorMsg");
    }

    formLoading() {
        this.formButton.attr("disabled", true);
        this.formButton.addClass("disabled");
        this.formSpinner.addClass("spinner-grow");
    }
    formRelieveLoading() {
        this.formButton.attr("disabled", false);
        // this.formButton.addClass("disabled");
        this.formSpinner.removeClass("spinner-grow");
        this.formButton.removeClass("disabled");
        this.clearFormData();
    }

    /**
     * 获取表单数据
     */
    getFormData() {
        const data = {
            chang: this.changSelect.val(),
            ma: this.maSelect.val(),
            jun: this.junSelect.val(),
        };
        if (data.chang === '0' && data.ma === '0' && data.jun === "0") {
            this.showErrorMsg("没有选择任何玩家。");
            return null;
        }

        let arrNum = [];
        for (let key in data) {
            let n = parseInt(data[key]);
            if (n !== 0) {
                arrNum.push(n);
            }
        }

        if (arrNum.length === 3) {
            log.warn("数据添加失败,选项太多了");
            this.showErrorMsg("你应该最多只选择两个赢家。");
            return null;
        }
        if (arrNum.length === 2) {
            if (arrNum[0] !== arrNum[1]) {
                log.warn("数据添加失败,两个选项值不一致");
                this.showErrorMsg("选择的两个赢家金额不一致。");
                return null;
            }
        }
        if (arrNum.length === 1) {
            if (arrNum[0] === 50) {
                log.warn("选择的值不对,只存在一个数据的时候不应给为50")
                this.showErrorMsg("地主不可能只赢5毛钱。");
                return null;
            }
        }
        this.hideErrorMsg();
        return data;
    }


    showErrorMsg(msg) {
        this.isError = true;
        this.formCatd.addClass("border-danger");
        this.formErrorAlert.removeClass("d-none");
        this.formErrorMsg.text(msg);
    }
    
    hideErrorMsg() {
        if(this.isError){
            this.formCatd.removeClass("border-danger");
            this.formErrorAlert.addClass("d-none");
            this.formErrorMsg.text("");
        }
    }

    /**
     * 清空表单内容
     */
    clearFormData() {
        this.changSelect.val(0);
        this.maSelect.val(0);
        this.junSelect.val(0);
    }
}